home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- '''
- '''
- __author__ = 'Robert Ancell <bob27@users.sourceforge.net>'
- __license__ = 'GNU General Public License Version 2'
- __copyright__ = 'Copyright 2005-2006 Robert Ancell'
- import board
- CHECK = '+'
- CHECKMATE = '#'
- MOVE = '-'
- TAKE = 'x'
- CASTLE_SHORT = 'o-o'
- CASTLE_LONG = 'o-o-o'
- _typeToLAN = {
- board.PAWN: 'P',
- board.KNIGHT: 'N',
- board.BISHOP: 'B',
- board.ROOK: 'R',
- board.QUEEN: 'Q',
- board.KING: 'K' }
- _lanToType = { }
- for pieceType, character in _typeToLAN.iteritems():
- _lanToType[character] = pieceType
- _lanToType[character.lower()] = pieceType
-
-
- class DecodeError(Exception):
- '''
- '''
- pass
-
-
- def _checkLocation(location):
- '''
- '''
- if len(location) != 2:
- raise DecodeError('Invalid length location')
- len(location) != 2
- if location[0] < 'a' or location[0] > 'h':
- raise DecodeError('Invalid rank')
- location[0] > 'h'
- if location[1] < '0' or location[1] > '8':
- raise DecodeError('Invalid file')
- location[1] > '8'
- return location
-
-
- def decode(colour, move):
- """Decode a long algebraic format move.
-
- 'colour' is the colour of the player making the move (board.WHITE or board.BLACK).
- 'move' is the move description (string).
-
- Returns a tuple containing (start, end, piece, moveType, promotionType, result)
- 'start' is the location being moved from (string, e.g. 'a1', 'h8').
- 'end' is the location being moved to (string, e.g. 'a1', 'h8').
- 'piece' is the piece being moved (board.PAWN, board.ROOK, ... or None if not specified).
- 'moveType' is a flag to show if this move takes an oppoenent piece (MOVE, TAKE or None if not specified).
- 'promotionType' is the piece type to promote to (board.ROOK, board.KNIGHT, ... or None if not specified).
- 'check' is the result after the move (CHECK, CHECKMATE or None if not specified).
-
- Raises DecodeError if the move is unable to be decoded.
- """
- pieceType = None
- promotionType = None
- moveType = None
- result = None
- m = move
- if colour is board.WHITE:
- baseFile = '1'
- else:
- baseFile = '8'
- if m == CASTLE_SHORT:
- return ('e' + baseFile, 'g' + baseFile, None, None, None, None)
- if m == CASTLE_LONG:
- return ('e' + baseFile, 'c' + baseFile, None, None, None, None)
- if len(m) < 1:
- raise DecodeError('Too short')
- len(m) < 1
-
- try:
- pieceType = _lanToType[m[0]]
- except KeyError:
- m == CASTLE_LONG
- m == CASTLE_LONG
- m == CASTLE_SHORT
- pieceType = None
- except:
- m == CASTLE_LONG
-
- m = m[1:]
- if len(m) < 2:
- raise DecodeError('Too short')
- len(m) < 2
-
- try:
- start = _checkLocation(m[:2])
- except DecodeError:
- m == CASTLE_LONG
- e = m == CASTLE_LONG
- m == CASTLE_SHORT
- if pieceType is None:
- raise e
- pieceType is None
- m = move
- start = _checkLocation(m[:2])
- pieceType = None
- except:
- m == CASTLE_LONG
-
- m = m[2:]
- if len(m) < 1:
- raise DecodeError('Too short')
- len(m) < 1
- if len(m) < 2:
- raise DecodeError('Too short')
- len(m) < 2
- end = _checkLocation(m[:2])
- m = m[2:]
- if len(m) > 0:
- if m[0] == '=':
- if len(m) < 2:
- raise DecodeError('Too short')
- len(m) < 2
-
- try:
- promotionType = _lanToType[m[1]]
- except KeyError:
- None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
- None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
- raise DecodeError('Unknown promotion type')
- except:
- None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
-
- m = m[2:]
- else:
-
- try:
- promotionType = _lanToType[m[0]]
- except KeyError:
- None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
- None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
- except:
- None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
-
- m = m[1:]
-
- if len(m) == 1:
- if m == CHECK or m == CHECKMATE:
- result = m
- m = ''
-
- elif len(m) == 2:
- if m == '++':
- result = CHECKMATE
- m = ''
-
-
- if len(m) != 0:
- raise DecodeError('Extra characters')
- len(m) != 0
- return (start, end, pieceType, moveType, promotionType, result)
-
-
- def encode(colour, start, end, piece = None, moveType = None, promotionType = None, result = None):
- """Encode a long algebraic format move.
-
- 'start' is the location being moved from (string, e.g. 'a1', 'h8').
- 'end' is the location being moved to (string, e.g. 'a1', 'h8').
- 'piece' is the piece being moved (board.PAWN, board.ROOK, ... or None if not specified).
- 'moveType' is a flag to show if this move takes an oppoenent piece (MOVE, TAKE or None if not specified).
- 'promotionType' is the piece type to promote to (board.ROOK, board.KNIGHT, ... or None if not specified).
- 'check' is the result after the move (CHECK, CHECKMATE or None if not specified).
-
- Returns a string describing this move.
- """
-
- try:
- _checkLocation(start)
- _checkLocation(end)
- except DecodeError:
- raise TypeError("Invalid values for 'start' and 'end'")
-
- string = ''
- if piece is not None:
- string += _typeToLAN[piece]
-
- string += start
- if moveType != None:
- string += moveType
-
- string += end
- if promotionType != None:
- if False:
- string += '='
-
- string += _typeToLAN[promotionType].lower()
-
- if result is not None:
- string += result
-
- return string
-
-